﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ANGFLib;

namespace ANGF
{
    public partial class FormEditCollectionGroup : Form
    {
        public FormEditCollectionGroup()
        {
            InitializeComponent();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (comboBoxKind.SelectedIndex < 0)
            {
                MessageBox.Show(this, "種類を選択してください", this.Text, MessageBoxButtons.OK);
                return;
            }
            if (string.IsNullOrWhiteSpace(this.textBoxName.Text))
            {
                MessageBox.Show(this, "名前を入力してください", this.Text, MessageBoxButtons.OK);
                return;
            }
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        internal string GetResultText()
        {
            if (comboBoxKind.SelectedIndex < 0) return "";
            if (string.IsNullOrWhiteSpace(this.textBoxName.Text)) return "";
            var item = (Collection)this.comboBoxKind.Items[comboBoxKind.SelectedIndex];
            return "#CG " + item.Id + " " + Guid.NewGuid().ToString() + " \"" + textBoxName.Text + "\"\r\n";
        }

        private void comboBoxKind_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0) return;
            e.DrawBackground();
            var item = (Collection)this.comboBoxKind.Items[e.Index];
            string s = item.HumanReadableName + " (" + item.Id + ")";
            e.Graphics.DrawString(s, e.Font, new SolidBrush(e.ForeColor), e.Bounds);
            e.DrawFocusRectangle();
        }

        private void FormEditCollectionGroup_Load(object sender, EventArgs e)
        {
            comboBoxKind.Items.Clear();
            foreach (var n in Collection.List.Values)
            {
                comboBoxKind.Items.Add(n);
            }
            if (Collection.List.Values.Count > 0) comboBoxKind.SelectedIndex = 0;
        }
    }
}
